<?php
/**
 * Timetable Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\curriculum;

use app\admin\model\curriculum\SchoolTimetable as SchoolTimetableModels;

Class SchoolTimetable {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'title') ) {
            $map[] = ['title', 'LIKE', $get['title'].'%'];
        }
        if ( get($get, 'cid') ) {
            $map[] = ['cid', '=', $get['cid']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return SchoolTimetableModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增课程表信息
     * @param array $data 数据包
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $schoolTimetable = SchoolTimetableModels::create($data, ['title','duration','cycle','start_cycle_time','end_cycle_time','live_url','delayed_url','cid']);
        return $schoolTimetable->isEmpty()?false:$schoolTimetable->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @return boolean
     */
    public function destory( $data )
    {
        return SchoolTimetableModels::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 教师管理编辑
     * @param SchoolTimetableModels $model
     * @param array $data
     */
    public function save(SchoolTimetableModels $model, array $data)
    {
        $model->title = $data['title'];
        $model->cid = $data['cid'];
        $model->cycle = $data['cycle'];
        $model->start_cycle_time = $data['start_cycle_time'];
        $model->end_cycle_time = $data['end_cycle_time'];
        $model->duration = $data['duration'];
        $model->delayed_url = $data['delayed_url'];
        $model->live_url = $data['live_url'];
        return $model->save();
    }

}